<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Pose of a widget</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d3/d81/tutorial_contrib_root.html">Tutorials for contrib modules</a></li><li class="navelem"><a class="el" href="../../d7/df9/tutorial_table_of_content_viz.html">OpenCV Viz</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Pose of a widget </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../d5/d7d/tutorial_launching_viz.html">Launching Viz</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../d7/d72/tutorial_transformations.html">Transformations</a></p>
<h2>Goal </h2>
<p>In this tutorial you will learn how to</p>
<ul>
<li>Add widgets to the visualization window</li>
<li>Use Affine3 to set pose of a widget</li>
<li>Rotating and translating a widget along an axis</li>
</ul>
<h2>Code </h2>
<p>You can download the code from <a href="https://github.com/opencv/opencv_contrib/tree/master/modules/viz/samples/widget_pose.cpp">here</a>. </p><div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d0/d7e/viz_8hpp.html">opencv2/viz.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d2/d28/calib3d_8hpp.html">opencv2/calib3d.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> help()</div><div class="line">{</div><div class="line">    cout</div><div class="line">    &lt;&lt; <span class="stringliteral">&quot;--------------------------------------------------------------------------&quot;</span>   &lt;&lt; endl</div><div class="line">    &lt;&lt; <span class="stringliteral">&quot;This program shows how to visualize a cube rotated around (1,1,1) and shifted &quot;</span></div><div class="line">    &lt;&lt; <span class="stringliteral">&quot;using Rodrigues vector.&quot;</span>                                                      &lt;&lt; endl</div><div class="line">    &lt;&lt; <span class="stringliteral">&quot;Usage:&quot;</span>                                                                       &lt;&lt; endl</div><div class="line">    &lt;&lt; <span class="stringliteral">&quot;./widget_pose&quot;</span>                                                                &lt;&lt; endl</div><div class="line">    &lt;&lt; endl;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line">    help();</div><div class="line"></div><div class="line">    <a class="code" href="../../d6/d32/classcv_1_1viz_1_1Viz3d.html">viz::Viz3d</a> myWindow(<span class="stringliteral">&quot;Coordinate Frame&quot;</span>);</div><div class="line"></div><div class="line">    myWindow.showWidget(<span class="stringliteral">&quot;Coordinate Widget&quot;</span>, <a class="code" href="../../df/d63/classcv_1_1viz_1_1WCoordinateSystem.html">viz::WCoordinateSystem</a>());</div><div class="line"></div><div class="line">    <a class="code" href="../../d6/d8e/classcv_1_1viz_1_1WLine.html">viz::WLine</a> axis(<a class="code" href="../../dc/d84/group__core__basic.html#ga3d79ceeb4419bccd0308dfdf1cd31435">Point3f</a>(-1.0f,-1.0f,-1.0f), <a class="code" href="../../dc/d84/group__core__basic.html#ga3d79ceeb4419bccd0308dfdf1cd31435">Point3f</a>(1.0f,1.0f,1.0f));</div><div class="line">    axis.setRenderingProperty(<a class="code" href="../../dc/d5c/group__viz__widget.html#ggae4df8152c2d8639b915eccd0bb90f8aca06fc87e04c15fbfad2977314751a8738">viz::LINE_WIDTH</a>, 4.0);</div><div class="line">    myWindow.showWidget(<span class="stringliteral">&quot;Line Widget&quot;</span>, axis);</div><div class="line"></div><div class="line">    <a class="code" href="../../d2/d17/classcv_1_1viz_1_1WCube.html">viz::WCube</a> cube_widget(<a class="code" href="../../dc/d84/group__core__basic.html#ga3d79ceeb4419bccd0308dfdf1cd31435">Point3f</a>(0.5,0.5,0.0), <a class="code" href="../../dc/d84/group__core__basic.html#ga3d79ceeb4419bccd0308dfdf1cd31435">Point3f</a>(0.0,0.0,-0.5), <span class="keyword">true</span>, <a class="code" href="../../d4/dba/classcv_1_1viz_1_1Color.html#a7b7b90d7bb2994782725c55e70d7f83f">viz::Color::blue</a>());</div><div class="line">    cube_widget.setRenderingProperty(<a class="code" href="../../dc/d5c/group__viz__widget.html#ggae4df8152c2d8639b915eccd0bb90f8aca06fc87e04c15fbfad2977314751a8738">viz::LINE_WIDTH</a>, 4.0);</div><div class="line">    myWindow.showWidget(<span class="stringliteral">&quot;Cube Widget&quot;</span>, cube_widget);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> rot_vec = <a class="code" href="../../d3/d63/classcv_1_1Mat.html#a0b57b6a326c8876d944d188a46e0f556">Mat::zeros</a>(1,3,<a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>);</div><div class="line">    <span class="keywordtype">float</span> translation_phase = 0.0, translation = 0.0;</div><div class="line"></div><div class="line">    rot_vec.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(0, 0) += (<span class="keywordtype">float</span>)CV_PI * 0.01f;</div><div class="line">    rot_vec.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(0, 1) += (<span class="keywordtype">float</span>)CV_PI * 0.01f;</div><div class="line">    rot_vec.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(0, 2) += (<span class="keywordtype">float</span>)CV_PI * 0.01f;</div><div class="line"></div><div class="line">    translation_phase += (float)CV_PI * 0.01f;</div><div class="line">    translation = <a class="code" href="../../d0/de1/group__core.html#ga53a8656033a51db64caa72ee9d4e93b4">sin</a>(translation_phase);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> rot_mat;</div><div class="line">    <a class="code" href="../../d9/d0c/group__calib3d.html#ga61585db663d9da06b68e70cfbf6a1eac">Rodrigues</a>(rot_vec, rot_mat);</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;rot_mat = &quot;</span> &lt;&lt; rot_mat &lt;&lt; endl;</div><div class="line">    <a class="code" href="../../dd/d99/classcv_1_1Affine3.html">Affine3f</a> pose(rot_mat, <a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(translation, translation, translation));</div><div class="line">    <a class="code" href="../../dd/d99/classcv_1_1Affine3.html">Affine3f</a> pose2(pose.matrix);</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;pose = &quot;</span> &lt;&lt; pose.matrix &lt;&lt; endl;</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;pose = &quot;</span> &lt;&lt; pose2.matrix &lt;&lt; endl;</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">    <span class="keywordflow">while</span>(!myWindow.wasStopped())</div><div class="line">    {</div><div class="line">        <span class="comment">/* Rotation using rodrigues */</span></div><div class="line">        rot_vec.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(0,0) += (<span class="keywordtype">float</span>)CV_PI * 0.01f;</div><div class="line">        rot_vec.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(0,1) += (<span class="keywordtype">float</span>)CV_PI * 0.01f;</div><div class="line">        rot_vec.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(0,2) += (<span class="keywordtype">float</span>)CV_PI * 0.01f;</div><div class="line"></div><div class="line">        translation_phase += (float)CV_PI * 0.01f;</div><div class="line">        translation = <a class="code" href="../../d0/de1/group__core.html#ga53a8656033a51db64caa72ee9d4e93b4">sin</a>(translation_phase);</div><div class="line"></div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> rot_mat1;</div><div class="line">        <a class="code" href="../../d9/d0c/group__calib3d.html#ga61585db663d9da06b68e70cfbf6a1eac">Rodrigues</a>(rot_vec, rot_mat1);</div><div class="line"></div><div class="line">        <a class="code" href="../../dd/d99/classcv_1_1Affine3.html">Affine3f</a> pose1(rot_mat1, <a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(translation, translation, translation));</div><div class="line"></div><div class="line">        myWindow.setWidgetPose(<span class="stringliteral">&quot;Cube Widget&quot;</span>, pose1);</div><div class="line"></div><div class="line">        myWindow.spinOnce(1, <span class="keyword">true</span>);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --><h2>Explanation </h2>
<p>Here is the general structure of the program:</p>
<ul>
<li>Create a visualization window. <div class="fragment"><div class="line">viz::Viz3d myWindow(<span class="stringliteral">&quot;Coordinate Frame&quot;</span>);</div></div><!-- fragment --></li>
<li>Show coordinate axes in the window using CoordinateSystemWidget. <div class="fragment"><div class="line">myWindow.showWidget(<span class="stringliteral">&quot;Coordinate Widget&quot;</span>, viz::WCoordinateSystem());</div></div><!-- fragment --></li>
<li>Display a line representing the axis (1,1,1). <div class="fragment"><div class="line">viz::WLine axis(<a class="code" href="../../dc/d84/group__core__basic.html#ga3d79ceeb4419bccd0308dfdf1cd31435">Point3f</a>(-1.0f,-1.0f,-1.0f), <a class="code" href="../../dc/d84/group__core__basic.html#ga3d79ceeb4419bccd0308dfdf1cd31435">Point3f</a>(1.0f,1.0f,1.0f));</div><div class="line">axis.setRenderingProperty(<a class="code" href="../../dc/d5c/group__viz__widget.html#ggae4df8152c2d8639b915eccd0bb90f8aca06fc87e04c15fbfad2977314751a8738">viz::LINE_WIDTH</a>, 4.0);</div><div class="line">myWindow.showWidget(<span class="stringliteral">&quot;Line Widget&quot;</span>, axis);</div></div><!-- fragment --></li>
<li>Construct a cube. <div class="fragment"><div class="line">viz::WCube cube_widget(<a class="code" href="../../dc/d84/group__core__basic.html#ga3d79ceeb4419bccd0308dfdf1cd31435">Point3f</a>(0.5,0.5,0.0), <a class="code" href="../../dc/d84/group__core__basic.html#ga3d79ceeb4419bccd0308dfdf1cd31435">Point3f</a>(0.0,0.0,-0.5), <span class="keyword">true</span>, viz::Color::blue());</div><div class="line">cube_widget.setRenderingProperty(<a class="code" href="../../dc/d5c/group__viz__widget.html#ggae4df8152c2d8639b915eccd0bb90f8aca06fc87e04c15fbfad2977314751a8738">viz::LINE_WIDTH</a>, 4.0);</div><div class="line">myWindow.showWidget(<span class="stringliteral">&quot;Cube Widget&quot;</span>, cube_widget);</div></div><!-- fragment --></li>
<li>Create rotation matrix from rodrigues vector <div class="fragment"><div class="line">rot_vec.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(0,0) += CV_PI * 0.01f;</div><div class="line">rot_vec.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(0,1) += CV_PI * 0.01f;</div><div class="line">rot_vec.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(0,2) += CV_PI * 0.01f;</div><div class="line"></div><div class="line">...</div><div class="line"></div><div class="line">Mat rot_mat;</div><div class="line"><a class="code" href="../../d9/d0c/group__calib3d.html#ga61585db663d9da06b68e70cfbf6a1eac">Rodrigues</a>(rot_vec, rot_mat);</div></div><!-- fragment --></li>
<li>Use Affine3f to set pose of the cube. <div class="fragment"><div class="line"><a class="code" href="../../d0/de1/group__core.html#gae10a81cba3f3232648be48e28759a7bc">Affine3f</a> pose(rot_mat, <a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(translation, translation, translation));</div><div class="line">myWindow.setWidgetPose(<span class="stringliteral">&quot;Cube Widget&quot;</span>, pose);</div></div><!-- fragment --></li>
<li>Animate the rotation using wasStopped and spinOnce <div class="fragment"><div class="line"><span class="keywordflow">while</span>(!myWindow.wasStopped())</div><div class="line">{</div><div class="line">    ...</div><div class="line"></div><div class="line">    myWindow.spinOnce(1, <span class="keyword">true</span>);</div><div class="line">}</div></div><!-- fragment --></li>
</ul>
<h2>Results </h2>
<p>Here is the result of the program.</p>
<p> 
<div align="center">
<iframe width="420" height="315" src="https://www.youtube.com/embed/22HKMN657U0" frameborder="0" allowfullscreen></iframe>
</div>
 </p>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:39 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
